/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.entitydataconvertor;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bff.designtime.generator.VmGeneratorContext;
import com.inspur.edp.bff.designtime.generator.entity.VmEntityGenContext;
import com.inspur.edp.bff.designtime.generator.entitydataconvertor.method.*;
import com.inspur.edp.bff.spi.AbstractDataConvertor;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import com.inspur.edp.das.commonmodel.entity.GspCommonObject;
import com.inspur.edp.formserver.viewmodel.GspViewObject;
import lombok.var;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.ArrayList;
import java.util.Arrays;

public class VMObjectDataConvertorGenerator extends ClassGenerator {

    private GspViewObject vmObject;
    private GspCommonObject targetObject;
    private VmGeneratorContext vmGeneratorContext;
    private VmEntityGenContext entityGenContext;
    private boolean isRoot;

    public VMObjectDataConvertorGenerator(GspViewObject vmObject, GspCommonObject targetObject,
                                          VmGeneratorContext vmGeneratorContext, VmEntityGenContext entityGenContext, boolean isRoot) {
        super(entityGenContext.getCompilationUnitInfo());
        this.vmObject = vmObject;
        this.targetObject = targetObject;
        this.vmGeneratorContext = vmGeneratorContext;
        this.entityGenContext = entityGenContext;
        this.isRoot = isRoot;
    }


    protected VMObjectDataConvertorGenContext getContext() {
        return (VMObjectDataConvertorGenContext) (
                (super.getContext() instanceof VMObjectDataConvertorGenContext) ? super.getContext() : null);
    }

    @Override
    protected ClassGeneratorContext createClassInfoContext() {
        VMObjectDataConvertorGenContext genContext = new VMObjectDataConvertorGenContext(vmObject,
                targetObject, vmGeneratorContext, entityGenContext);
        if (isRoot) {
            vmGeneratorContext.setMainObjectDataConvertorGenContext(genContext);
        }
        return genContext;
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
        return new ArrayList<Modifier.ModifierKeyword>(
                Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
    }

    @Override
    protected TypeInfo setParentClass() {
        var typeInfo = new TypeInfo("AbstractDataConvertor");
        typeInfo.setTypePackageName("com.inspur.edp.bff.spi");
        return typeInfo;
    }

    @Override
    protected String getName() {
        return String.format("%1$sConvertor", vmObject.getCode());
    }

    public final String getClassName() {
        return getName();
    }

//    @Override
//    protected String getNameSpace() {
//        return entityGenContext.CommonModel.GetEntityAssemblyInfo().DefaultNamespace;
//    }

    // region 方法生成
    @Override
    protected ArrayList<ClassMethodGenerator> createConstructorGenerators() {
        return new ArrayList<ClassMethodGenerator>(java.util.Arrays
                .asList(new ClassMethodGenerator[]{new VODataConvertorCtorGenerator(getName())}));
    }

    // endregion 方法生成

    @Override
    protected ArrayList<BaseGenerator> createClassExtendChildGenrators() {
        ArrayList<BaseGenerator> list = new ArrayList<BaseGenerator>();
        for (IGspCommonObject childObj : vmObject.getContainChildObjects()) {
            GspViewObject item = (GspViewObject) childObj;
            if (item == null)
                continue;
            if (item.getIsVirtual() || item.getMapping().getTargetMetadataId().equals( getContext().getVmGeneratorContext()
                    .getRefBizEntity().getID())==false) {
                continue;
            }
            Object tempVar = ((GspBusinessEntity) ((targetObject.getBelongModel() instanceof GspBusinessEntity)
                    ? targetObject.getBelongModel() : null)).findObjectById(item.getMapping().getTargetObjId());
            GspBizEntityObject childEntityObject = (GspBizEntityObject) (
                    (tempVar instanceof GspBizEntityObject) ? tempVar : null);

            //targetObject.GetChildObjectById(item.Mapping.TargetObjId) as GspBizEntityObject;
            VMObjectDataConvertorGenerator childGen = new VMObjectDataConvertorGenerator(item,
                    childEntityObject, vmGeneratorContext, entityGenContext, false);
            getContext().getChildGenerators().put(item.getID(), childGen);
            list.add(childGen);
        }
        return list;
    }

    @Override
    protected ArrayList<ClassMethodGenerator> createMethodGenerators() {
        return new ArrayList<ClassMethodGenerator>(java.util.Arrays.asList(
                new ClassMethodGenerator[]{new GetChildDataConvertorMethodGenerator(getContext()),
                    new MappingEntityDataPropertiesGenerator(getContext()),
                    new MappingDataFormVoGenerator(getContext()),
                    new CheckChildGenerator(vmObject),
                    new MappingMultiLanguagePropsFromVoGenerator(getContext()),
                    new MappingMultiLanguagePropsGenerator(getContext())
                }));
    }
}
